<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
      class="formList"
    >
      <el-row>
        <el-form-item label="供应商" prop="supplierUid">
          <el-select
            v-model="queryParams.supplierUid"
            placeholder="请选择供应商"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in supplierUidOptions"
              :key="dict.supplierUid"
              :label="dict.supplierName"
              :value="dict.supplierUid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input
            v-model="queryParams.contactPerson"
            placeholder="请输入联系人"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="supplierPhone">
          <el-input
            v-model="queryParams.supplierPhone"
            placeholder="请输入入驻手机号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="状态" prop="dataStatus">
          <el-select
            v-model="queryParams.dataStatus"
            placeholder="请选择状态"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in dataStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发布状态" prop="isAuth">
          <el-select
            v-model="queryParams.isAuth"
            placeholder="请选择发布状态"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in isAuthOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTimeArray">
          <el-date-picker
            v-model="queryParams.createTimeArray"
            type="daterange"
            align="right"
            value-format="yyyy-MM-dd"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
      </el-row>
      <el-row >
        <el-form-item style="margin-left: 22px">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索
          </el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置
          </el-button
          >
        </el-form-item>
      </el-row>
    </el-form>

    <el-row class="row-mbinfo" style="margin-left: 0px; margin-right: 0px;">

    </el-row>
    <div class="divTable">
      <el-table
        v-loading="loading"
        :data="infoList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"/>

        <el-table-column
          label="LOGO"
          align="center"
          :show-overflow-tooltip="true"
          prop="supplierLogUrl"
        >
          <template slot-scope="scope">
            <el-image v-if="scope.row.supplierLogUrl != null && scope.row.supplierLogUrl != '' " slot="reference"
                      :src="scope.row.supplierLogUrl"
                      class="image-rows" :preview-src-list="[scope.row.supplierLogUrl]"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="供应商名称" align="center" prop="supplierName"/>
        <el-table-column label="联系人" align="center" prop="contactPerson"/>
        <el-table-column label="联系电话" align="center" prop="supplierPhone"/>

        <el-table-column
          label="状态"
          align="center"
          prop="dataStatus"
          :formatter="dataStatusFormat"
        />
        <el-table-column
          label="发布状态"
          align="center"
          prop="isAuth"
          :formatter="isAuthFormat"
        />
        <el-table-column
          label="认证状态"
          align="center"
          prop="settledState"
          :formatter="supplierSettledStateFormat"
        />

        <el-table-column label="申请时间" align="center" prop="createTime"/>
        <el-table-column label="审核时间" align="center" prop="auditDate"/>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="startStatusUpdate(scope.row)"
              v-if="scope.row.settledState === '2'"
              v-hasPermi="['supplier:info:edit']"
            >审核
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-zoom-in"
              @click="handleShowDetail(scope.row)"
              v-hasPermi="['supplier:info:query']"
            >查看
            </el-button>
          </template>
        </el-table-column>

      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </div>
    <!-- 查看供应商资料对话框 -->
    <el-dialog
      :title="viewTitle"
      :visible.sync="viewOpen"
      width="1000px"
      append-to-body
    >
      <el-form ref="detailForm">
        <div>
          <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i>
          <div class="MmsUiBlockTitle___title___2C-H-1-44-1">
            <span>基础信息</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-row>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="供应商编号">
                  <span>{{ showData.supplierNo }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
            </el-col>
            <el-col :span="8"></el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="供应商名称">
                  <span>{{ showData.supplierName }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
            </el-col>
            <el-col :span="8"></el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="品类">
                  <span>{{ showData.mainCategory }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="品牌">
                <span>{{ showData.supplierBrand }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="LOGO">
              <el-image
                :src="showData.supplierLogUrl"
                style="width: 100px; height: 100px"
                :z-index="zIndex"
                :preview-src-list="[showData.supplierLogUrl]"
              ></el-image>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item>
              <el-form-item label="简介">
                <span>{{ showData.supplierRemark }}</span>
              </el-form-item>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="售后说明">
              <span>{{ showData.orderService }}</span>
            </el-form-item>
          </el-row>
        </div>

        <div>
          <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i>
          <div class="MmsUiBlockTitle___title___2C-H-1-44-1">
            <span>联系方式</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-row>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="联系人">
                  <span>{{ showData.contactPerson }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="手机号">
                <span>{{ showData.supplierPhone }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">

            </el-col>
          </el-row>

          <el-row>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="地址">
                  <span>{{ regionName }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="详细地址">
                <span>{{ showData.supplierAddress }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="邮箱">
                <span>{{ showData.supplierEmail }}</span>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>

          </el-row>
        </div>

        <div>
          <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i>
          <div class="MmsUiBlockTitle___title___2C-H-1-44-1">
            <span>其他信息</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="营业执照">
                <el-image
                  v-for="(item, index) in licenseImgUrl"
                  :key="index"
                  style="width: 100px; height: 100px"
                  :src="item.url"
                  :preview-src-list="[item.url]"
                  :z-index="zIndex"
                >
                </el-image>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="宣传图">
                <el-image
                  v-for="(item, index) in bannerImage"
                  :key="index"
                  style="width: 100px; height: 100px"
                  :src="item.url"
                  :preview-src-list="[item.url]"
                  :z-index="zIndex"
                >
                </el-image>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="发布状态">
                  <span>{{ showDataIsAuth }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="发布人账号">
                <span>{{ showData.authUpdateUid }}</span>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="更新时间">
                  <span>{{ showData.updateTime }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="更新人账号">
                <span>{{ showData.updateName }}</span>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>

            <el-col :span="8">
              <el-form-item label="创建时间">
                <span>{{ showData.createTime }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="创建人账号">
                  <span>{{ showData.createName }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>

          </el-row>

          <el-row>

            <el-col :span="8">
              <el-form-item label="审核时间">
                <span>{{ showData.auditDate }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="审核人账号">
                  <span>{{ showData.auditName }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>

          </el-row>


        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail"
        >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 审核对话框-->
    <el-dialog
      :title="viewTitle"
      :visible.sync="auditDialogBox"
      width="1000px"
      append-to-body
    >
      <el-form ref="detailForm">
        <div>
          <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i>
          <div class="MmsUiBlockTitle___title___2C-H-1-44-1">
            <span>基础信息</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-row>
            <el-col :span="8">
            </el-col>
            <el-col :span="8"></el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="供应商名称">
                  <span>{{ showData.supplierName }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
            </el-col>
            <el-col :span="8"></el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="品类">
                  <span>{{ showData.mainCategory }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="品牌">
                <span>{{ showData.supplierBrand }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="LOGO">
              <el-image
                :src="showData.supplierLogUrl"
                style="width: 100px; height: 100px"
                :z-index="zIndex"
                :preview-src-list="[showData.supplierLogUrl]"
              ></el-image>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item>
              <el-form-item label="简介">
                <span>{{ showData.supplierRemark }}</span>
              </el-form-item>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="售后说明">
              <span>{{ showData.orderService }}</span>
            </el-form-item>
          </el-row>
        </div>

        <div>
          <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i>
          <div class="MmsUiBlockTitle___title___2C-H-1-44-1">
            <span>联系方式</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-row>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="联系人">
                  <span>{{ showData.contactPerson }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="手机号">
                <span>{{ showData.supplierPhone }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">

            </el-col>
          </el-row>

          <el-row>
            <el-col :span="8">
              <el-form-item>
                <el-form-item label="地址">
                  <span>{{ regionName }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="详细地址">
                <span>{{ showData.supplierAddress }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="邮箱">
                <span>{{ showData.supplierEmail }}</span>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>

          </el-row>
        </div>


        <div>
          <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i>
          <div class="MmsUiBlockTitle___title___2C-H-1-44-1">
            <span>审核</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-form-item label="审核意见">
            <el-input type="textarea" v-model="form.desc"></el-input>
          </el-form-item>
          <el-form-item>
            <div style="margin-left: 70%">
              <el-button type="primary" @click="supplierAudit(showData.supplierUid,1)">同意</el-button>
              <el-button @click="supplierAudit(showData.supplierUid,-1)">驳回</el-button>
            </div>

          </el-form-item>
        </div>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
  import {
    listInfo,
    getInfo,
    delInfo,
    addInfo,
    updateInfo,
    updateDataStatus,
    getSupplierList,
    updateIsAuth,
    updateSettledState
  } from '@/api/supplier/info'

  import {getRegion} from '@/api/common/regin'
  import imageArray from '@/components/ImageUpload/imageArray'
  import imageList from '@/components/ImageUpload/imageList'
  import imageUpload from '@/components/ImageUpload'

  export default {
    name: 'Info',
    components: {
      imageList,
      imageUpload,
      imageArray
    },
    data() {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        //发布状态翻译
        showDataIsAuth: '',
        //审核弹框
        auditDialogBox:false,
        //查看详细省市区翻译
        regionName: '',
        //图片预览
        zIndex: 3000,
        //文本域自动调节
        autosize: true,
        //宣传图
        bannerImage: [],
        //营业执照
        licenseImgUrl: [],
        region: [],
        // 地区信息
        options: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 供应商资料表格数据
        infoList: [],
        // 弹出层标题
        title: '',
        //查看详细标题
        viewTitle: '',
        //选中数组的名字
        names: [],
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 状态字典
        dataStatusOptions: [],
        // 发布状态字典
        isAuthOptions: [],
        //供应商列表
        supplierUidOptions: [],
        //认证状态
        supplierSettledState:[],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          supplierName: null,
          supplierPhone: null,
          contactPerson: null,
          dataStatus: null,
          supplierUid: null,
          isAuth: null,
          createTime: null,
          settledState: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
      this.getDicts('sys_data_status').then((response) => {
        this.dataStatusOptions = response.data
      })
      this.getDicts('supplier_is_auth').then((response) => {
        this.isAuthOptions = response.data
      })
      this.getDicts('supplier_settled_state').then((response) => {
        this.supplierSettledState = response.data
      })
      getSupplierList("all").then((response) => {
        this.supplierUidOptions = response.data
      })
    },
    methods: {
      /** 查询供应商资料列表 */
      getList() {
        this.loading = true
        listInfo(this.queryParams).then((response) => {
          this.infoList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 状态字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus)
      },
      // 发布状态字典翻译
      isAuthFormat(row, column) {
        return this.selectDictLabel(this.isAuthOptions, row.isAuth)
      },
      // 认证状态字典翻译
      supplierSettledStateFormat(row, column) {
        return this.selectDictLabel(this.supplierSettledState, row.settledState)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          supplierUid: null,
          platformUid: null,
          supplierNo: null,
          mainCategory: null,
          supplierBrand: null,
          supplierName: null,
          supplierLogUrl: null,
          servePhone: null,
          supplierRemark: null,
          supplierEmail: null,
          supplierPhone: null,
          supplierProvince: null,
          supplierCity: null,
          supplierArea: null,
          supplierAddress: null,
          contactPerson: null,
          contactPhone: null,
          dataStatus: null,
          isAuth: null,
          applyUser: null,
          applyTime: null,
          licenseImgUrl: null,
          authTime: null,
          authUpdateUid: null,
          createUser: null,
          updateUser: null,
          updateTime: null,
          createTime: null,
          storeLng: null,
          storeLat: null,
          bannerImage: null,
          juli: null,
          auditRemark: null,
          orderService: null,
          dataSort: null,
          region: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.supplierUid)
        this.names = selection.map((item) => item.supplierName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        //用于禁用删除
        if (this.multiple == false) {
          for (var i = 0; i < selection.length; i++) {
            var element = selection[i]
            if ('1' === element.dataStatus) {
              this.multiple = true
              break
            }
          }
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        //获取地区信息
        getRegion().then((response) => {
          this.options = response.data
        })
        this.reset()
        this.open = true
        this.title = '添加供应商资料'
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true
        this.viewTitle = '查看供应商资料'
        this.showData = row

        getRegion().then((response) => {
          this.options = response.data
          var regionName = ''
          var region = this.options
          region.forEach(function (value, key, iterable) {
            if (value.value == row.supplierProvince) {
              regionName = value.label
              value.children.forEach(function (value, key, iterable) {
                if (value.value == row.supplierCity) {
                  regionName += value.label
                  value.children.forEach(function (value, key, iterable) {
                    if (value.value == row.supplierArea) {
                      regionName += value.label
                      return false
                    }
                  })
                  return false
                }
              })
              return false
            }
          })
          this.regionName = regionName
        })
        let this_ = this
        this.dataStatusOptions.forEach(function (value, key) {
          if (value.dictValue == row.isAuth) {
            this_.showDataIsAuth = value.dictLabel
            return false
          }
        })

        if (row.bannerImage != null) {
          this.bannerImage = row.bannerImage.split(',').map((item, index) => {
            return {url: item, name: index}
          })
        }

        if (row.licenseImgUrl != null) {
          this.licenseImgUrl = row.licenseImgUrl.split(',').map((item, index) => {
            return {url: item, name: index}
          })
        }
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const supplierUid = row.supplierUid || this.ids
        getInfo(supplierUid).then((response) => {
          this.form = response.data
          this.open = true
          this.title = '修改供应商资料'
          //获取地区信息
          getRegion().then((response) => {
            this.options = response.data
          })
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate((valid) => {
          let area = this.$refs['cascaderAddr'].getCheckedNodes()[0].path
          this.form.supplierProvince = area[0]
          this.form.supplierCity = area[1]
          this.form.supplierArea = area[2]
          if (valid) {
            if (this.form.supplierUid != null) {
              updateInfo(this.form).then((response) => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          'supplier/info/export',
          {
            ...this.queryParams
          },
          `supplier_info.xlsx`
        )
      },
      startStatusUpdate(row){
        this.auditDialogBox = true
        this.showData = row
      },
      supplierAudit(supplierUid,settledState){
        updateSettledState(supplierUid,settledState).then((ressponse)=>{
          this.msgSuccess('审核成功')
          this.auditDialogBox = false
          this.getList()
        })
      }
    }
  }
</script>

<style scoped>
  .MmsUiBlockTitle___line___27lhp1-44-1 {
    display: inline-block;
    background: #407cff;
    width: 2px;
    height: 16px;
    margin-right: 8px;
  }

  .MmsUiBlockTitle___title___2C-H-1-44-1 {
    display: inline-block;
    font-size: 20px;
    color: #333;
    position: relative;
  }
</style>
